// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//引入图标
import './assets/font/iconfont.css'

//IE兼容问题
import promise from 'es6-promise'
promise.polyfill()


//引入全局样式
import './assets/css/global.css'

//引入 UI  
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

//引入axios
import axios from 'axios'
//配置请求根路径
axios.defaults.baseURL = 'http://api_dev.wanxikeji.cn'
//axios请求拦截器
axios.interceptors.request.use(config => {
  return config
})

axios.interceptors.response.use(res => {
  if (res.data.code === 2000) {
    return res
  } else {
    
    ElementUI.Message({
      message: res.data.msg,
      type: 'error'
    });
    if(res.data.code === 3000){
      location.href =  '/';
    }
    return Promise.reject(res);
  }
})

//挂在到vue原型
Vue.prototype.$http = axios
// Vue.prototype.$uploading = uploading
Vue.use(ElementUI)
Vue.config.productionTip = false




/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') next();
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) {
    ElementUI.Message({message:'token过期，请重新登录',type:'warning'});
    setTimeout(()=>{
      next('./login')
    },1000)
  } else { next() }
});


export default router;